// 多人游戏玩家类
export class Player {
  constructor(id, name, isAI = false, seatIndex = 0) {
    this.id = id;
    this.name = name;
    this.isAI = isAI;
    this.seatIndex = seatIndex; // 座位索引（0,1,2）
    this.cards = [];
    this.status = 'online'; // online, offline, disconnected
    this.isReady = false;
    this.starCoins = 100; // 虚拟币，多人游戏中暂不使用
    this.joinedAt = new Date();
  }

  // 序列化为JSON（用于网络传输）
  toJSON(includeCards = false) {
    const playerData = {
      id: this.id,
      name: this.name,
      isAI: this.isAI,
      seatIndex: this.seatIndex,
      status: this.status,
      isReady: this.isReady,
      cardCount: this.cards.length,
      joinedAt: this.joinedAt
    };

    // 只有在需要时才包含手牌信息（通常用于该玩家自己）
    if (includeCards) {
      playerData.cards = this.cards.map(card => card.toJSON());
    }

    return playerData;
  }

  // 从JSON反序列化
  static fromJSON(json, cards = []) {
    const player = new Player(json.id, json.name, json.isAI, json.seatIndex);
    player.status = json.status || 'online';
    player.isReady = json.isReady || false;
    player.cards = cards;
    player.joinedAt = json.joinedAt ? new Date(json.joinedAt) : new Date();
    return player;
  }

  // 获取安全的玩家信息（不包含手牌）
  getSafeInfo() {
    return {
      id: this.id,
      name: this.name,
      isAI: this.isAI,
      seatIndex: this.seatIndex,
      status: this.status,
      isReady: this.isReady,
      cardCount: this.cards.length
    };
  }
}